home *** CD-ROM | disk | FTP | other *** search
- (macro cons-stream
- (lambda (x)
- `(cons ,(cadr x)
- (delay ,(caddr x)))))
-
- (define (head x) (car x))
-
- (define (tail x) (force (cdr x)))
-
- (define the-empty-stream
- ((named-lambda (empty-stream)
- (cons-stream 'empty-stream (empty-stream)))))
-
- (define (empty-stream? x) (eq? (head x) 'empty-stream))
-
- (define (stream? x)
- (and (pair? x) (delayed-object? (cdr x))))
-
- (define (stream->list z)
- (if (empty-stream? z)
- '()
- (cons (head z) (stream->list (tail z)))))
-
- (define (list->stream z)
- (if (null? z)
- the-empty-stream
- (cons-stream (car z) (list->stream (cdr z)))))
-
- (define (stream-map p s)
- (if (empty-stream? s)
- the-empty-stream
- (cons-stream (p (head s))
- (stream-map p (tail s)))))
-
- (define (stream-for-each p s)
- (if (empty-stream? s)
- the-empty-stream
- (begin (p (head s))
- (stream-for-each p (tail s)))))
-
- (define (stream-append f s)
- (if (empty-stream? f)
- s
- (cons-stream (head f)
- (stream-append (tail f) s))))
-
- (define (stream-filter p s)
- (cond ((empty-stream? s) the-empty-stream)
- ((p (head s)) (cons-stream (head s)
- (stream-filter p (tail s))))
- (else (stream-filter p (tail s)))))
-
- (define (stream-ref n s)
- (while (> n 0)
- (set! n (-1+ n))
- (set! s (tail s)))
- (head s))
-
- (define stream-nth stream-ref)
-
-